/*
 * $action v1.0 Copyright(c) 2016.6 by CSS WeidongWang
 */
function isnull(str){return (str==null||str==""||str=="undefine");};
function closeDialog(){
	var list = $.dialog.list;
	for( var i in list ){
	    list[i].close();
	}
}
function closeDialogCloseTab(tabId){
	closeTab(tabId)
	closeDialog();
}
function closeDialogRefreshTabForm(){
	$navTab.refreshCurrentTabForm();
	closeDialog();
}
function refreshCurrentTabForm(){
	$navTab.refreshCurrentTabForm();
}
function refreshTab(tabId){
	if(tabId)
		$navTab.refreshTab(tabId);
	else
		$navTab.refreshCurrentTab();
}
function closeDialogRefreshTab(tabId){
	if(tabId)
		$navTab.refreshTab(tabId);
	else
		$navTab.refreshCurrentTab();
	closeDialog();
}
function closeTab(tabId){
	if(tabId)
		$navTab.closeTab(tabId);
	else
		$navTab.closeCurrentTab();
}
function noCloseNoRefresh(){
}
function actionModel(){
		url="";
		title="";
		callback="";
		tabId="";
		afterExec="";
}
;(function($){
	var actMessage = {
		act : '你确定要操作这条记录吗？',
		batch : '你确定要操作这些记录吗？',
		alertBatch:'请选中一条数据！'
		},
		methods = {
			template: function(data,params,form){
				if(params.callback){
				  params.callback(data,form);
				  return;
				}
				if(data.result==0){
				  if(isnull(params.afterExec))
				  	params.afterExec=refreshCurrentTabForm;
				    params.afterExec(params.tabId,data,form);
					$css.tip(data.msg);
				}else{
				  $css.alert(data.msg);
				}
			}
		};
 	$action = {
        downloadBatch: function(el,params){
            var ids=$newTable.getCheckbox(el);
            if(ids.length>0){
                var url=params.url, title=params.title;
                if(isnull(title)) title=actMessage.batch;
                $css.confirm(title,function(){
                    document.location=url+"?" + "ids=" + ids;
                });
            } else {
                $css.alert(actMessage.alertBatch);
            }
            return false;
        },
        exportBatch: function(el,params){
        	debugger;
            var formParms = $("#user_form", $navTab.focus()).serialize();
			var url=params.url, title=params.title;
			if(isnull(title)) title=actMessage.batch;
			$css.confirm(title,function(){
				document.location=url+"?" + formParms;
                /*$css.post(url,formParms,function(data){
                    $css.tip("操作成功！");
                },'json');*/
			});
            return false;
        },
 		//params={url:''必须, callback:''有则调用, data:{json参数}, afterExec:''有则调用, title:'' 不传默认为'你确定要操作这些记录吗？'}
 		execBatch:function(el,params){
 			var ids=$newTable.getCheckbox(el);
 			if(ids.length>0){
 				var url=params.url, title=params.title;
 				if(isnull(title)) title=actMessage.batch;
 				$css.confirm(title,function(){
				  $css.post(url,{'ids':ids},function(data){
						methods.template(data,params);
				  },'json');
				});
			} else {
				$css.alert(actMessage.alertBatch);
			}
			return false;
 		},
 		execBatchThenBack:function(el,params){
 			var ids=$newTable.getCheckbox(el);
 			if(ids.length>0){
 				var url=params.url, title=params.title;
 				if(isnull(title)) title=actMessage.batch;
 				$css.confirm(title,function(){
				  $css.post(url,{'ids':ids},function(data){
                      closeDialog();
                      $css.tip("操作成功！");
                  },'json');
				});
			} else {
				$css.alert(actMessage.alertBatch);
			}
			return false;
 		},
 		upd:function(el,params){
 			var ids=el && $newTable.getSelected(el);
 			if(ids && ids.length>0){
 				if(params.url.indexOf('?')==-1){params.url += "?";}
 				params.url += "&ids="+ids;
 				$css.openDialog(params);
 				return false;
 			} else {
 				$css.alert(actMessage.alertBatch);
 			}
 			return false;
 		},
 		updBatch:function(el,params){
 			var ids=$newTable.getCheckbox(el);
 			if(ids.length>0){
 				if(params.url.indexOf('?')==-1){params.url += "?";}
 				params.url += "&ids="+ids;
 				$css.openDialog(params);
 				return false;
 			} else {
 				$css.alert(actMessage.alertBatch);
 			}
 			return false;
 		},
 		exec:function(el,params){
 			var url=params.url, title=params.title;
 			if(isnull(title)) title=actMessage.act;
 			$css.confirm(title,function(){
			  $css.post(url,params.data,function(data){
					methods.template(data,params);
			  },'json');
			});
 		},
 		execLable:function(el,params){
 			var url=params.url, title=params.title;
 			if(isnull(title)) title=actMessage.act;
 			$css.confirm(title,function(){
			  $css.post(url,params.data,function(data){
                  var lableId = params.callbackParam.lableId;
                  var userId = params.callbackParam.userId;
                  params.callback(lableId,userId);
                  return;
			  },'json');
			});
 		},
 		execCall:function(params){
 			$css.post(params.url,params.data,function(data){
 				if(data.result==0){
 					if(params.callback){
					  params.callback.call();
					  return;
					}
				}else{
				  $css.alert(data.msg);
				}
			},'json');
 		},
 		execNoTip:function(params){
 			$css.post(params.url,params.data,function(data){
				methods.template(data,params);
			},'json');
 		},
 		submit: function(form, callback, tabId, afterExec) {
	    var $form = $(form);
	    if (!$form.valid()) 
	    	return false;
	    blockUI();
	    $css.post($form.attr("action"),$form.serialize(),function(data){
	    	var params=new actionModel();
	    	params.callback = callback;
	    	params.tabId=tabId;
	    	params.afterExec=afterExec;
	    	if(isnull(params.afterExec))
				  	params.afterExec=closeDialogRefreshTabForm;
	    	methods.template(data,params,form);
	    	unblockUI();
	    },'json');
	    return false;
		},
 		query: function(form) {
	   	$navTab.submitForm(form);
			return false;
		},
		ajaxSubmit : function(url, param, $el) {
			//$el.html('<center><div class="ptLoading">加载中...</div></center>');
			blockUI();
			$.post(url, param, function(data) {
				$el.html(data);
				unblockUI();
			});
			return false;
		},
  };
})(jQuery)